﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain.Logic.AI;
using Brain.Logic3D.PathFinding;
using Microsoft.Xna.Framework;

namespace Brain.Logic3D.AI
{
    public class ArtificialIntelligence3D : ArtificialIntelligence, IDrawable
    {
        public WorldObject WorldObject { get; set; }
        public bool ShouldDraw { get; set; }

        public PathFinder3D Finder;

        public Map3D Map;

        public float Height = 5;

        public float MinDistance = 5;

        private Vector3 targetPosition;
        public bool TargetChanged = false;

        public bool SetTargetPosition(Vector3 value)
        {
            float distance = Vector3.Distance(this.Position, value);
            if (distance > MinDistance)
            {
                targetPosition = value;
                TargetChanged = true;
                return true;
            }
            return false;
        }

        public ArtificialIntelligence3D(AIInfo info, PathNode3D startNode, Map3D Map, WorldObject wo)
            : base(info)
        {
            this.Finder = new PathFinder3D(Map);
            Finder.Height = 20f;
            Finder.FoundWay += new SetVoid(Finder_FoundWay);

            this.WorldObject = wo;
            this.Position = startNode.Triangle.GetMidPoint();
            targetPosition = this.Position;

            this.WorldObject.Position = this.Position;
            this.Data = new AIData3D(this, info);
            ShouldDraw = true;

            WorldObject.SetCalculate(true, true, true, true);

            this.CurrentNode = startNode;
        }

        void Finder_FoundWay()
        {
            this.currentIndex = Finder.WayList.Count - 1;
        }

        public PathNode3D CurrentNode;
        public int currentIndex;
        public override void Update()
        {
            base.Update();

            Finder.Update();

            if (this.Finder.Status == PathFinder3DStatus.Finished)
            {
                PathNodeInfo3D info = Finder.WayList[currentIndex];
                Vector3 dir = Vector3Util.Direction(this.WorldObject.Position, info.Position);
                this.WorldObject.Position += dir;
                this.CurrentNode = info.ParentNode3D;

                if (Vector3.Distance(this.WorldObject.Position, info.Position) < 1f)
                {
                    currentIndex--;

                    if (currentIndex < 0)
                    {
                        this.Finder.Stop();
                        currentIndex = 0;
                    }
                }
            }
            if (TargetChanged && this.Finder.Status == PathFinder3DStatus.Stopped ||
                TargetChanged && this.Finder.Status == PathFinder3DStatus.NoPath)
            {
                Finder.Search(CurrentNode, targetPosition + new Vector3(0, Height, 0));
            }

            if (WorldObject.ShouldUpdate)
            {
                this.Position = WorldObject.Position;
                WorldObject.Update();
            }
        }

        public virtual void Draw()
        {
            if (WorldObject.ShouldDraw)
            {
                WorldObject.Draw();
            }
        }
    }
}
